import axios from "axios";
import { ElMessage } from "element-plus";
// 封装 axiso
let instance = axios.create({
  baseURL: "/admin",
  timeout: 5000, //设置超时时间
});
// 设置请求拦截器
instance.interceptors.request.use((config) => {
  let token = localStorage.getItem("token");
  if (token) {
    config.headers.token = token;
  }
  return config;
});
// 设置响应拦截器
instance.interceptors.response.use((res) => {
  if (res.data?.data?.token) {
    localStorage.setItem("token", res.data.data.token);
  }
  ElMessage({
    message: res.data.message,
    type: res.data.code === "200" ? "success" : "error",
    duration: 1000,
  });
  return res.data;
});
export default instance;
